import scrapy
from beautiful.items import BeautifulItem

class BeautifulSpider(scrapy.Spider):
    name = "beautiful"
    allowed_domains = ["www.mm131.com/"]
    
    start_urls = [
        'http://www.mm131.com/xinggan/',
        'http://www.mm131.com/qingchun/',
        'http://www.mm131.com/xiaohua/',
        'http://www.mm131.com/chemo/',
        'http://www.mm131.com/qipao/',
        'http://www.mm131.com/mingxing/'
    ]

    def parse(self,response):
        list = response.css(".list_left dd:not(.page)")
        for img in list:
            imgname = img.css("a::text").extract_first()
            imgurl = img.css("a::attr(href)").extract_first()
            imgurl2 = str(imgurl)
            print(imgurl2)
            next_url = response.css(".page_en:nth-last-child(2)::attr(href)").extract_first()
            if next_url is not None:
                # next page
                yield response.follow(next_url,callback=self.parse)

            yield scrapy.Request(imgurl2,callback=self.content)
    
    def content(self,response):
        item = BeautifulItem()
        item['name'] = response.css(".content h2::text").extract_first()
        item['ImgUrl'] = response.css(".content-pic img::attr(src)").extract_first()
        yield item
        #extract picture ,adn save to directory
        #print(item['ImgUrl'])
        next_url = response.css(".page-ch:last-child::attr(href)").extract_first()

        if next_url is not None:
            # next page
            yield response.follow(next_url,callbak=self.content)